﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace Easydp.Models
{
    public class OrderManager
    {
        #region SELECT Region
        public Entities.TT_Orders GetAllOrders(int AgentID)
        {
            Entities.TT_Orders obj = new Easydp.Models.Entities.TT_Orders();
            using (SqlText selection = new SqlText("SELECT * FROM TT_Orders "))
            {
                selection.AddParameter("@AgentID", AgentID);
                while (selection.Read())
                {

                    obj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                    obj.Amount = Convert.ToInt32(selection["Amount"].ToString());
                    obj.CreateOn = Convert.ToDateTime(selection["CreateOn"].ToString());
                    obj.IsCHD = Convert.ToBoolean(selection["IsCHD"].ToString());
                    obj.OrderID = Convert.ToInt32(selection["OrderID"].ToString());
                    obj.OrderNum = selection["OrderNum"].ToString();
                    obj.PayMoney = Convert.ToDecimal(selection["PayMoney"].ToString());
                    obj.PayPoundage = Convert.ToDecimal(selection["PayPoundage"].ToString());
                    obj.Pew = selection["Pew"].ToString();
                    obj.PhotoAuthCode = selection["PhotoAuthCode"].ToString();
                    obj.StartStation = selection["StartStation"].ToString();
                    obj.EndStation = selection["EndStation"].ToString();
                    obj.TicketDate = Convert.ToDateTime(selection["TicketDate"].ToString());
                    obj.TotalMoney = Convert.ToDecimal(selection["TotalMoney"].ToString());
                    obj.TrainNumber = selection["TrainNumber"].ToString();
                    obj.UnitPrice = Convert.ToDecimal(selection["UnitPrice"].ToString());
                    obj.UserID = Convert.ToInt32(selection["UserID"].ToString());
                    obj.UserName = selection["UserName"].ToString();
                    obj.UserTelphone = selection["UserTelphone"].ToString();
                    obj.Remark = selection["Remark"].ToString();
                }
            }
            return obj;
        }
        /// <summary>
        /// 得到列表
        /// </summary>
        public List<Entities.TT_Orders> GetListOrders(int AgentID)
        {
            List<Entities.TT_Orders> list=new List<Easydp.Models.Entities.TT_Orders>();
            using (SqlStoredProcedure selectlist = new SqlStoredProcedure("Pro_Orders_Pan_SelectIncoming"))
            {
                selectlist.AddParameter("@AgentID", AgentID);
                while (selectlist.Read())
                {
                    Entities.TT_Orders order = new Easydp.Models.Entities.TT_Orders();
                    order.AgentID = int.Parse(selectlist["AgentID"].ToString());
                    order.Amount = Convert.ToInt32(selectlist["Amount"].ToString());
                    order.CreateOn = Convert.ToDateTime(selectlist["CreateOn"].ToString());
                    order.EndStation = selectlist["EndStation"].ToString();
                    order.IsCHD = Convert.ToBoolean(selectlist["IsCHD"].ToString());
                    order.OrderID = Convert.ToInt32(selectlist["OrderID"].ToString());
                    order.OrderNum = selectlist["OrderNum"].ToString();
                    order.PayMoney = Convert.ToDecimal(selectlist["PayMoney"].ToString());
                    order.PayPoundage = Convert.ToDecimal(selectlist["PayPoundage"].ToString());
                    order.Pew = selectlist["Pew"].ToString();
                    if (selectlist["PhotoAuthCode"] != null)
                    {
                        order.PhotoAuthCode = selectlist["PhotoAuthCode"].ToString();
                    }
                    order.StartStation = selectlist["StartStation"].ToString();
                    order.TicketDate = Convert.ToDateTime(selectlist["TicketDate"].ToString());
                    order.TotalMoney = Convert.ToDecimal(selectlist["TotalMoney"].ToString());
                    order.TrainNumber = selectlist["TrainNumber"].ToString();
                    order.UnitPrice = Convert.ToDecimal(selectlist["UnitPrice"].ToString());
                    order.UserID = Convert.ToInt32(selectlist["UserID"].ToString());
                    order.UserName = selectlist["UserName"].ToString();
                    order.UserTelphone = selectlist["UserTelphone"].ToString();
                    order.OrderState = Convert.ToInt32(selectlist["OrderState"].ToString());
                    order.Remark = selectlist["Remark"].ToString();
                    list.Add(order);
                }
            }
            return list;
        }



        public Entities.TT_Orders getmodel(string sqlWhere)
        {
            string sql = "SELECT * FROM TT_Orders ";
            if (sqlWhere.ToString() != "")
            {
                sql += "where"+sqlWhere ;
            }
            Entities.TT_Orders obj = new Easydp.Models.Entities.TT_Orders();

            using (SqlText selection = new SqlText(sql))
            {
                while (selection.Read())
                {
                    obj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                    obj.Amount = Convert.ToInt32(selection["Amount"].ToString());
                    obj.CreateOn = Convert.ToDateTime(selection["CreateOn"].ToString());
                    obj.IsCHD = Convert.ToBoolean(selection["IsCHD"].ToString());
                    obj.OrderID = Convert.ToInt32(selection["OrderID"].ToString());
                    obj.OrderNum = selection["OrderNum"].ToString();
                    obj.PayMoney = Convert.ToDecimal(selection["PayMoney"].ToString());
                    obj.PayPoundage = Convert.ToDecimal(selection["PayPoundage"].ToString());
                    obj.Pew = selection["Pew"].ToString();
                    obj.PhotoAuthCode = selection["PhotoAuthCode"].ToString();
                    obj.StartStation = selection["StartStation"].ToString();
                    obj.EndStation = selection["EndStation"].ToString();
                    obj.TicketDate = Convert.ToDateTime(selection["TicketDate"].ToString());
                    obj.TotalMoney = Convert.ToDecimal(selection["TotalMoney"].ToString());
                    obj.TrainNumber = selection["TrainNumber"].ToString();
                    obj.UnitPrice = Convert.ToDecimal(selection["UnitPrice"].ToString());
                    obj.UserID = Convert.ToInt32(selection["UserID"].ToString());
                    obj.UserName = selection["UserName"].ToString();
                    obj.OrderState = Convert.ToInt32(selection["OrderState"].ToString());
                    obj.UserTelphone = selection["UserTelphone"].ToString();
                    obj.Remark = selection["Remark"].ToString();
                }
            }
            return obj;
        }
        /// <summary>
        /// 得到模型
        /// </summary>
        public List<Entities.TT_Orders> GetModel(int AgentID, string ordernum, string inDate, string outDate)
        {
            List<Entities.TT_Orders> list = new List<Easydp.Models.Entities.TT_Orders>();
            string SqlWhere = "SELECT * FROM TT_Orders where AgentID=" + AgentID + " and  OrderState = 2 ";
            if (ordernum != "" && ordernum!=null)
            {
                SqlWhere += " and OrderNum = '" + ordernum + "' ";
            }
            if (inDate != "yyyy-mm-dd" && inDate != "" && inDate != null)
            {
                SqlWhere += " and CreateOn >='" + inDate + "'";
            }
            if (outDate != "yyyy-mm-dd" && outDate != "" && outDate != null)
            {
                SqlWhere += " and CreateOn <= '" + outDate + "'";
            }
            SqlWhere += " order by CreateOn";

           using (SqlText selection = new SqlText(SqlWhere.ToString()))
           {
               while (selection.Read())
               {
                   Entities.TT_Orders obj = new Easydp.Models.Entities.TT_Orders();
                   obj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                   obj.Amount = Convert.ToInt32(selection["Amount"].ToString());
                   obj.CreateOn = Convert.ToDateTime(selection["CreateOn"].ToString());
                   obj.IsCHD = Convert.ToBoolean(selection["IsCHD"].ToString());
                   obj.OrderID = Convert.ToInt32(selection["OrderID"].ToString());
                   obj.OrderNum = selection["OrderNum"].ToString();
                   obj.PayMoney = Convert.ToDecimal(selection["PayMoney"].ToString());
                   obj.PayPoundage = Convert.ToDecimal(selection["PayPoundage"].ToString());
                   obj.Pew = selection["Pew"].ToString();
                   obj.PhotoAuthCode = selection["PhotoAuthCode"].ToString();
                   obj.StartStation = selection["StartStation"].ToString();
                   obj.EndStation = selection["EndStation"].ToString();
                   obj.TicketDate = Convert.ToDateTime(selection["TicketDate"].ToString());
                   obj.TotalMoney = Convert.ToDecimal(selection["TotalMoney"].ToString());
                   obj.TrainNumber = selection["TrainNumber"].ToString();
                   obj.UnitPrice = Convert.ToDecimal(selection["UnitPrice"].ToString());
                   obj.UserID = Convert.ToInt32(selection["UserID"].ToString());
                   obj.UserName = selection["UserName"].ToString();
                   obj.OrderState = Convert.ToInt32(selection["OrderState"].ToString());
                   obj.UserTelphone = selection["UserTelphone"].ToString();
                   obj.Remark = selection["Remark"].ToString();
                   list.Add(obj);
               }
           }
           return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetDataset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select OrderID,OrderNum,UserID,AgentID,TicketDate,StartStation,EndStation,TrainNumber,UserName,UserTelphone,Pew,UnitPrice,PayPoundage,TotalMoney,PayMoney,PhotoAuthCode,OrderState,Amount,IsCHD,CreateOn,Remark ");
            strSql.Append(" FROM TT_Orders ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得所有数据列表
        /// </summary>

        public List<Entities.TT_Orders> GetList(int AgentID, string ordernum, string inDate, string outDate)
        {
            List<Entities.TT_Orders> list = new List<Easydp.Models.Entities.TT_Orders>();
            string SqlWhere = "SELECT * FROM TT_Orders where AgentID=" + AgentID + " ";
            if (ordernum != "" && ordernum != null)
            {
                SqlWhere += " and OrderNum = '" + ordernum + "' ";
            }
           
            if (inDate != "yyyy-mm-dd" && inDate != "" && inDate != null)
            {
                SqlWhere += " and CreateOn >='" + inDate + "'";
            }
            if (outDate != "yyyy-mm-dd" && outDate != "" && outDate != null)
            {
                SqlWhere += " and CreateOn <= '" + outDate + "'";
            }
            if ((inDate == "yyyy-mm-dd" || inDate == "" || inDate == null) && (outDate == "yyyy-mm-dd" || outDate == "" || outDate == null))
            {
                SqlWhere += " and CreateOn >= (getdate()-3)";
            }
            SqlWhere += " order by CreateOn";

            using (SqlText selection = new SqlText(SqlWhere.ToString()))
            {
                while (selection.Read())
                {
                    Entities.TT_Orders obj = new Easydp.Models.Entities.TT_Orders();
                    obj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                    obj.Amount = Convert.ToInt32(selection["Amount"].ToString());
                    obj.CreateOn = Convert.ToDateTime(selection["CreateOn"].ToString());
                    obj.IsCHD = Convert.ToBoolean(selection["IsCHD"].ToString());
                    obj.OrderID = Convert.ToInt32(selection["OrderID"].ToString());
                    obj.OrderNum = selection["OrderNum"].ToString();
                    obj.PayMoney = Convert.ToDecimal(selection["PayMoney"].ToString());
                    obj.PayPoundage = Convert.ToDecimal(selection["PayPoundage"].ToString());
                    obj.Pew = selection["Pew"].ToString();
                    obj.PhotoAuthCode = selection["PhotoAuthCode"].ToString();
                    obj.StartStation = selection["StartStation"].ToString();
                    obj.EndStation = selection["EndStation"].ToString();
                    obj.TicketDate = Convert.ToDateTime(selection["TicketDate"].ToString());
                    obj.TotalMoney = Convert.ToDecimal(selection["TotalMoney"].ToString());
                    obj.TrainNumber = selection["TrainNumber"].ToString();
                    obj.UnitPrice = Convert.ToDecimal(selection["UnitPrice"].ToString());
                    obj.UserID = Convert.ToInt32(selection["UserID"].ToString());
                    obj.UserName = selection["UserName"].ToString();
                    obj.OrderState = Convert.ToInt32(selection["OrderState"].ToString());
                    obj.UserTelphone = selection["UserTelphone"].ToString();
                    obj.Remark = selection["Remark"].ToString();
                    list.Add(obj);
                }
            }
            return list;
        }
  
#endregion

        #region UPDATE
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Entities.TT_Orders model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TT_Orders set ");
            strSql.Append("OrderNum=@OrderNum,");
            strSql.Append("UserID=@UserID,");
            strSql.Append("AgentID=@AgentID,");
            strSql.Append("TicketDate=@TicketDate,");
            strSql.Append("StartStation=@StartStation,");
            strSql.Append("EndStation=@EndStation,");
            strSql.Append("TrainNumber=@TrainNumber,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("UserTelphone=@UserTelphone,");
            strSql.Append("Pew=@Pew,");
            strSql.Append("UnitPrice=@UnitPrice,");
            strSql.Append("PayPoundage=@PayPoundage,");
            strSql.Append("TotalMoney=@TotalMoney,");
            strSql.Append("PayMoney=@PayMoney,");
            strSql.Append("PhotoAuthCode=@PhotoAuthCode,");
            strSql.Append("OrderState=@OrderState,");
            strSql.Append("Amount=@Amount,");
            strSql.Append("IsCHD=@IsCHD,");
            strSql.Append("CreateOn=@CreateOn,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where OrderID=@OrderID ");
            using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@OrderNum",model.OrderNum);

                updation.AddParameter("@UserID", model.UserID);
                updation.AddParameter("@AgentID", model.AgentID);
                updation.AddParameter("@TicketDate", model.TicketDate);
                updation.AddParameter("@StartStation", model.StartStation);
                updation.AddParameter("@EndStation", model.EndStation);
                updation.AddParameter("@TrainNumber", model.TrainNumber);
                updation.AddParameter("@UserName", model.UserName);
                updation.AddParameter("@UserTelphone", model.UserTelphone);
                updation.AddParameter("@Pew", model.Pew);
                updation.AddParameter("@UnitPrice", model.UnitPrice);
                updation.AddParameter("@PayPoundage", model.PayPoundage);
                updation.AddParameter("@TotalMoney", model.TotalMoney);
                updation.AddParameter("@PayMoney", model.PayMoney);
                updation.AddParameter("@PhotoAuthCode", model.PhotoAuthCode);
                updation.AddParameter("OrderState", model.OrderState);
                updation.AddParameter("@Amount", model.Amount);
                updation.AddParameter("@IsCHD", model.IsCHD);
                updation.AddParameter("@CreateOn", model.CreateOn);
                updation.AddParameter("@Remark", model.Remark);
                return updation.ExecuteNonQuery();

            }
           
        }

        public int Update(int OrderID, int State)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TT_Orders set ");
            strSql.Append("OrderState=@OrderState");
            strSql.Append(" where OrderID=@OrderID ");
            using (SqlText updation = new SqlText(strSql.ToString()))
            {

                updation.AddParameter("@OrderID", OrderID);
                updation.AddParameter("@OrderState", State);
                return updation.ExecuteNonQuery();
            }
        }
        #endregion

        #region INSERT 
        public int Insert(Easydp.Models.Entities.TT_Orders obj)
        {
            int orderID=0;
            using (SqlStoredProcedure insertion = new SqlStoredProcedure("Pro_Orders_Pan_InsertNew"))
            {
                object userid = obj.UserID;
                if (obj.UserID <= 0)
                    userid = System.DBNull.Value;

                insertion.AddParameter("@UserID",userid);
                insertion.AddParameter("@AgentID", obj.AgentID);
                insertion.AddParameter("@TicketDate", obj.TicketDate);
                insertion.AddParameter("@StartStation", obj.StartStation);
                insertion.AddParameter("@EndStation", obj.EndStation);
                insertion.AddParameter("@TrainNumber", obj.TrainNumber);
                insertion.AddParameter("@UserName", obj.UserName);
                insertion.AddParameter("@UserTelphone", obj.UserTelphone);
                insertion.AddParameter("@Remark", obj.Remark);
                insertion.AddParameter("@Pew", obj.Pew);
                insertion.AddParameter("@UnitPrice", obj.UnitPrice);
                insertion.AddParameter("@TotalMoney", obj.TotalMoney);
                insertion.AddParameter("@PayMoney", obj.PayMoney);
                insertion.AddParameter("@PayPoundage", obj.PayPoundage);
                insertion.AddParameter("@Amount", obj.Amount);
                insertion.AddParameter("@isCHD", obj.IsCHD);
                try
                {
                    int flag = insertion.ExecuteNonQuery();
                    if (flag > 0)
                    {
                        obj.OrderID = insertion.SelectIdentity();
                        orderID = obj.OrderID;
                        (new PushFeeManager()).Insert(obj);
                    }
                }
                catch (Exception ex)
                {
                    orderID = -1;
                }
                    
            }
            return orderID;
        }
        #endregion

        #region Pay

        public Entities.TT_Orders GetWaitPayOrder(string orderid)
        {
            Entities.TT_Orders obj = new Easydp.Models.Entities.TT_Orders();
            using (SqlText selection = new SqlText(@"select a.* from TT_Orders a WHERE a.OrderState='1' AND a.OrderID = @OrderID"))
            {
                selection.AddParameter("@OrderID", orderid);
                while (selection.Read())
                {
                    obj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                    obj.Amount = Convert.ToInt32(selection["Amount"].ToString());
                    obj.CreateOn = Convert.ToDateTime(selection["CreateOn"].ToString());
                    obj.IsCHD = Convert.ToBoolean(selection["IsCHD"].ToString());
                    obj.OrderID = Convert.ToInt32(selection["OrderID"].ToString());
                    obj.OrderNum = selection["OrderNum"].ToString();
                    obj.PayMoney = Convert.ToDecimal(selection["PayMoney"].ToString());
                    obj.PayPoundage = Convert.ToDecimal(selection["PayPoundage"].ToString());
                    obj.Pew = selection["Pew"].ToString();
                    obj.PhotoAuthCode = selection["PhotoAuthCode"].ToString();
                    obj.StartStation = selection["StartStation"].ToString();
                    obj.EndStation = selection["EndStation"].ToString();
                    obj.TicketDate = Convert.ToDateTime(selection["TicketDate"].ToString());
                    obj.TotalMoney = Convert.ToDecimal(selection["TotalMoney"].ToString());
                    obj.TrainNumber = selection["TrainNumber"].ToString();
                    obj.UnitPrice = Convert.ToDecimal(selection["UnitPrice"].ToString());
                    if (!string.IsNullOrEmpty(selection["UserID"].ToString()))
                    {
                        obj.UserID = Convert.ToInt32(selection["UserID"].ToString());
                    }
                    obj.UserName = selection["UserName"].ToString();
                    obj.OrderState = Convert.ToInt32(selection["OrderState"].ToString());
                    obj.UserTelphone = selection["UserTelphone"].ToString();
                    obj.Remark = selection["Remark"].ToString();
                }
            }
            return obj;
        }

        #endregion
    }
}
